/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateless.MRP.SaleOrderProcess;

import entity.CRM.SaleOrder;
import entity.MRP.SOProcess.BackOrder;
import entity.Product;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import session.stateless.CRM.SaleOrderBeanLocal;

/**
 *
 * @author Vincent
 */
@Stateless
public class BackOrderSessionBean implements BackOrderSessionBeanLocal {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext (unitName = "M10-IS3102-ejbPU")
    private EntityManager entityManager; 
    @EJB
    SaleOrderBeanLocal saleOrderBeanLocal;
    
    @Override
    public Long createBackOrder(Long saleOrderId, List<Product> products, List<Long> quantities, Long leadTime){
        SaleOrder so = entityManager.find(SaleOrder.class, saleOrderId);
        
        BackOrder bo = new BackOrder();
        bo.setSaleOrder(so);
        Calendar c = Calendar.getInstance();
        String date = c.get(Calendar.DAY_OF_MONTH) + "/" + c.get(Calendar.MONTH + 1) + "/" + c.get(Calendar.YEAR);
        bo.setCreationDate(date);
        bo.setProducts(products);
        bo.setQuantities(quantities);
        bo.setLeadTime(leadTime);
        entityManager.persist(bo);
        return bo.getId();
    }
}